<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
    <meta http-equiv="Cache-Control" content="no-cache, must-revalidate"/>
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT"/>
    <title th:inline="text">[[${appName}]]</title>
    <link href="images/favicon.ico" rel="SHORTCUT ICON"/>
    <link th:href="@{${skinPath} + '/css.css'}" rel="stylesheet" type="text/css"/>
    <link th:href="@{${skinPath} + '/login.css'}" rel="stylesheet" type="text/css"/>
    <link type="text/css" rel="stylesheet" th:href="@{${skinPath} + '/jquery-ui/jquery-ui-1.10.4.css'}" />
    <script src="inc/common.js"></script>
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="js/jquery-migrate-1.2.1.min.js"></script>
    <script src="js/jquery.toaster.js" type="text/javascript"></script>
    <script src="js/jquery-ui/jquery-ui-1.10.4.min.js"></script>
    <script src="js/jquery.form.js"></script>
    <link href="js/jquery-alerts/jquery.alerts.css" rel="stylesheet" type="text/css" media="screen"/>
    <script src="js/jquery-alerts/jquery.alerts.js" type="text/javascript"></script>
    <script src="js/jquery-alerts/cws.alerts.js" type="text/javascript"></script>
    <script src="js/aes.min.js" type="text/javascript"></script>
    <script th:inline="javascript">
        // 不允许在框架页中打开
        if(top != self) {
            top.location = self.location;
        }

        // 浏览器是否合法
        var isBrowserValid = true;
        var browserInfo = "";

        // 判断是否为手机端，如果是则转至手机端界面
        var isMobile = false;
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
            isMobile = true;
        } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
            // isMobile = true;
        } else if (ua.match(/WeiBo/i) == "weibo") {
            isMobile = true;
        } else if (/(iPhone|iPad|iPod|iOS)/i.test(ua)) {
            isMobile = true;
        } else if (/(Android)/i.test(ua)) {
            isMobile = true;
        }
        if (isMobile) {
            window.location.href = "wap/index.jsp";
        }

        $(function () {
            [# th:if="${isRememberUserName}"]
            o("name").value = unescape(get_cookie("name"));
            if (o("name").value == "") {
                o("name").focus();
            }
            else {
                o("pwd").focus();
            }

            o("pwd").value = aesDecrypt(get_cookie("pwd"));
            if (o("pwd").value == "") {
                $('.isSavePwd').removeAttr("checked");
            } else {
                $('.isSavePwd').attr("checked", "checked");
            }
            [/]
        });

        function onunload() {
            var skincode = [[${skincode}]];
            var expdate = new Date();
            var expday = 60;
            expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * expday));
            document.cookie = "skincode" + "=" + skincode + ";expires=" + expdate.toGMTString();
            document.cookie = "name" + "=" + escape(o("name").value) + ";expires=" + expdate.toGMTString();
            if ($('.isSavePwd').is(":checked")) {
                document.cookie = "pwd" + "=" + aesMinEncrypt(o("pwd").value) + ";expires=" + expdate.toGMTString();
            } else {
                document.cookie = "pwd" + "=;expires=" + expdate.toGMTString();
            }
        }

        $(function () {
            controlPwdInputValue();
        });

        function controlPwdInputValue() {
            var input = document.getElementById('loginPwd');
            input.onfocus = function () {
                if (this.value == '请输入密码') {
                    this.value = '';
                    this.type = 'password';
                }
            };
            input.onblur = function () {
                if (!this.value) {
                    this.value = '请输入密码';
                }
            };
        }

        function onload() {
            checkWebEditInstalled();
        }
    </script>
</head>
<body onload="onload()" onunload="onunload()" scroll="no">
<div class="logincontent">
    <div class="login_topbox">
        <div class="login_topleft"><img th:src="@{${skinPath} + '/images/login/login_logo.png'}"/></div>
    </div>
    <div class="loginbar_frame">
        <div class="loginbar_computer_bg"></div>
        <div class="loginbar_bgbox">
            <div class="loginbar_posit ">
                <div class="loginbar_case">
                    <form id="loginForm" name="loginForm" method="post" action="doLogin.do">
                        <div id="errMsgBox" class="input_box1" style='display:none'>
                            <img id="errMsgImg" th:src="@{${skinPath} + '/images/login/login_errorico.png'}" width="18" height="18"/>
                            <span id="errMsgSpan">用户名不正确请重新输入</span>
                        </div>
                        <div class="input_box2">
                            <div class="span_user"></div>
                            <span></span>
                            <input type="text" id="loginName" name="name" value="请输入您的用户名" onKeyPress="return namePresskey(event)"/>
                        </div>
                        <div class="input_box2">
                            <div class="span_code"></div>
                            <span>
                                <input type="password" id="loginPwd" name="pwd" autocomplete="off" value="请输入您的密码"/>
                            </span>
                        </div>
                        <div class="input_box4">
                            <span th:if="${isRememberPwdDisplay}" th:inline="text">
                            <input type="checkbox" id="isSavePwd" name="isSavePwd" class="isSavePwd" checked="checked"/>
                            记住密码
                            </span>
                            <input name="signature" type="hidden"/>
                            <input name="op" type="hidden"/>
                            <input name="mainTitle" type="hidden" th:value="${mainTitle}"/>
                            <input name="mainPage" type="hidden" th:value="${mainPage}"/>
                            [(${tokenHidden})]
                            <span id="send_to_desktop" data-step="1" data-intro="在桌面生成快捷方式" data-position="top">
				                <a href="send_to_desktop.jsp" title="发送至桌面快捷方式"><img alt="发送至桌面快捷方式" th:src="@{/images/send_to_desktop.png}"/></a>
			                </span>
                        </div>
                        <div class="login_button" onClick="doLogin()"><a></a></div>
                        <div class="QRcode" title="Android手机直接扫描二维码,Apple手机扫描二维码进入App Store下载"><img th:src="@{${skinPath} + '/images/login/QRcode.png'}" width="75" height="21"/></div>
                        <div id="loginDownload" style="foat:left; width:120px;"><a id="linkDownload" href="activex/oa_client.exe">下载客户端</a>
                        </div>
                        <div style="float: left; width:80px;" th:if="${isPwdCanReset}"><a href="public/userResetPwd.do" title="通过邮箱重置密码">重置密码</a></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="qrCodeBox" style="position:absolute; display:none; width:139px; height:178px; background-color:white">
    <div id="androidQRCode">
        <img id="QRCodeimg" th:src="${'images/' + qrcode_mobile_png_path}"/>
        <div style="text-align:center;"><a th:href="${qrcode_andriod_download_path}">手机客户端</a><br/></div>
    </div>
</div>
<script>
    if (!isIE8) {
        var strObj = '<OBJECT ID="Spindial1" WIDTH=3 HEIGHT=3 style="visibility: hidden;"';
        strObj += ' CLASSID="CLSID:B3AF7FDF-4123-499A-B38E-EDCE3821C10F"';
        strObj += ' CODEBASE="activex/cloudym.CAB#Version=1,2,0,1"></OBJECT>';
        $('body').append(strObj);
    }
</script>
<script th:src="@{${skinPath} + '/login.js'}"></script>
<div class="logincontent-footer"></div>
</body>
<script th:inline="javascript">
    var isIEVersionValid = true;
    var browserIEMinVersion = [(${browserIEMinVersion})];

    // AES密钥 (需要前端和后端保持一致，十六位)
    var KEY = [[${pwdAesKey}]];
    // AES密钥偏移量 (需要前端和后端保持一致，十六位)
    var IV = [[${pwdAesIV}]];

    /**
     * 加密（需要先加载aes.min.js文件）
     * @param word
     * @returns {*}
     */
    function aesMinEncrypt(word){
        var _word = CryptoJS.enc.Utf8.parse(word),
            _key = CryptoJS.enc.Utf8.parse(KEY),
            _iv = CryptoJS.enc.Utf8.parse(IV);
        var encrypted = CryptoJS.AES.encrypt(_word, _key, {
            iv: _iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return encrypted.toString();
    }

    /**
     * 解密
     * @param word
     * @returns {*}
     */
    function aesDecrypt(word) {
        var _key = CryptoJS.enc.Utf8.parse(KEY);
        var _iv = CryptoJS.enc.Utf8.parse(IV);
        var decrypted = CryptoJS.AES.decrypt(word, _key, {
            iv: _iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return decrypted.toString(CryptoJS.enc.Utf8);
    }

    // 登录
    function doLogin() {
        if (!isBrowserValid) {
            jAlert("请勿使用" + browserInfo.toUpperCase() + "浏览器，请换用" + [[${browserValid}]] + ", 提示");
            return;
        }
        if (!isIEVersionValid) {
            jAlert("IE浏览器请使用" + browserIEMinVersion + "以上版本", "提示");
            return;
        }
        var userName = $('#loginName').val();
        $.ajax({
            type: "post",
            url: "doLogin.do",
            data: {
                name: userName,
                [[${pwdName}]]: aesMinEncrypt($('#loginPwd').val()),
                signature: o("signature").value,
                op: o("op").value,
                mainTitle: o("mainTitle").value,
                mainPage: o("mainPage").value,
                form_token: o("form_token").value,
                isSavePwd: o("isSavePwd").value
            },
            dataType: "html",
            beforeSend: function (XMLHttpRequest) {
                // $('#bodyBox').showLoading();
            },
            success: function (data, status) {
                // 过滤掉其它字符，只保留JSON字符串
                var m = data.match(/\{.*?\}/gi);
                if (m != null) {
                    if (m.length == 1) {
                        data = m[0];
                    }
                }

                try {
                    data = jQuery.parseJSON(data);
                } catch (e) {
                    consoleLog(data);
                    alert(data);
                    return;
                }

                if (data == null)
                    return;

                data.msg = data.msg.replace(/\\r/ig, "<BR>");

                if (data.ret == "0") {
                    $('#errMsgBox').show();
                    $('#errMsgImg').show();
                    $('#errMsgSpan').html(data.msg);
                    return;
                } else {
                    $('#errMsgBox').hide();
                    window.location.href = data.redirect;
                }
            },
            complete: function (XMLHttpRequest, status) {
                // $('#bodyBox').hideLoading();
            },
            error: function (XMLHttpRequest, textStatus) {
                // 请求出错处理
                consoleLog(XMLHttpRequest.responseText);
                alert(XMLHttpRequest.responseText);
            }
        });
    }

    function namePresskey(event) {
        var event = event || window.event;
        var keyCode = event.keyCode;
        if (event.keyCode == 13) {
            event.keyCode = 9;
            if (typeof event.stopPropagation != "undefined") {
                event.stopPropagation();
            } else {
                event.cancelBubble = true;
            }
            o("loginPwd").focus();
            return false;
        }
    }

    $(function () {
        $("#loginPwd").keydown(function (e) {
            var e = e || event,
                keycode = e.which || e.keyCode;
            if (keycode == 13) {
                doLogin();
            }
        });
    });

    function checkWebEditInstalled() {
        var ProgID = "FHtmlEdit.HtmlEdit.1";
        var bCtlLoaded = true;
        var webeditObj;
        try {
            webeditObj = new ActiveXObject(ProgID);
        } catch (e) {
            bCtlLoaded = false;
        }

        if (!bCtlLoaded) {
            var options = {};
            var selectedEffect = "pulsate";
            if (isWow64()) {
                $('#linkDownload').attr("href", "activex/oa_client.exe");
            }
            $("#loginDownload").show(selectedEffect, options, 1000, callback);
            $("#loginDownload").css('float', 'left');
            return;
        }

        // 检测控件版本
        var isCtlVerCorrect = true;
        if (webeditObj && typeof (webeditObj.GetVersion) == "undefined") {
            isCtlVerCorrect = false;
        } else {
            if (webeditObj && webeditObj.GetVersion() != "6.10") {
                isCtlVerCorrect = false;
            }
        }

        if (!isCtlVerCorrect) {
            $("#loginDownload").html("<a id='loginDownload' href=\"activex/oa_client.exe\">下载新版客户端</a>");
            $("#loginDownload").css('float', 'left');
        }
    }

    function callback() {
    }

    $(function () {
        $('#loginName').focus(function () {
            if ($('#loginName').val() == "请输入您的用户名") {
                $('#loginName').val("");
            }
        });
        $('#loginPwd').focus(function () {
            if ($('#loginPwd').val() == "请输入您的密码") {
                $('#loginPwd').val("");
            }
        });

        $('.QRcode').hover(function () {
                $('#qrCodeBox').show();
                $('#qrCodeBox').css("left", $('.QRcode').offset().left);
                $('#qrCodeBox').css("top", $('.QRcode').offset().top - $('#qrCodeBox').height() + 3);
            },
            function () {
                $('#qrCodeBox').hide();
            }
        );

        $('#qrCodeBox').hover(function () {
                $('#qrCodeBox').show();
                // $('#qrCodeBox').css("left", $('.QRcode').offset().left);
                // $('#qrCodeBox').css("top", $('.QRcode').offset().top - $('#qrCodeBox').height() + 3);
            },
            function () {
                $('#qrCodeBox').hide();
            }
        );
    });
    
    function showTip(msg) {
        var options = {
            'priority': 'info',
            'message': msg,
            'settings': {
                'toast': {
                    'css': {
                        'background': '#d4eefe',
                        'color': '#008ced',
                        'font-size': '15px',
                        'font-family': '宋体',
                        'filter': 'filter:alpha(opacity=80)',
                        'vertical-align': 'middle',
                        'border': '1px solid #a8deff',
                        '-moz-border-radius': '5px',
                        '-webkit-border-radius': '5px',
                        'border-radius': '5px',
                        'line-height': '20px',
                        'padding': '12px'
                    }
                },
                'toaster': {
                    'css': {
                        'min-width': '200px',
                        'max-width': '220px',
                        'height': '40px',
                        'position': 'fixed',
                        'top': '10px',
                        'left': '40%'
                    }
                }
            }
        };
        $.toaster(options);
    }
    
    $(document).ready(function () {
        var ieVer = parseInt(_IE);
        if (ieVer < browserIEMinVersion) {
            isIEVersionValid = false;
            jAlert("IE浏览器请使用" + browserIEMinVersion + "以上版本", "提示");
            return;
        }

        [# th:if="${isBrowserForbid}"]
            // 猎豹浏览器在request的agent中含有 LEBROWSER，但是通过js获取的agent中则没有
            var browserValid = [[${browserValid}]];
            var aryValid = browserValid.split(",");
            browserInfo = getBrowserInfo().toLowerCase();
            // 如果是chrome型的，则赋予browserType，以得到真正的浏览器类型
            if (browserInfo == "chrome") {
                browserInfo = [[${browserType}]];
            }
            isBrowserValid = false;
            for (var i = 0; i < aryValid.length; i++) {
                if (browserInfo == aryValid[i].toLowerCase()) {
                    isBrowserValid = true;
                }
            }
            if (!isBrowserValid) {
                jAlert("请勿使用" + browserInfo.toUpperCase() + "浏览器，请换用" + browserValid, "提示");
            }
            // consoleLog(navigator.userAgent.toLocaleLowerCase());
            if (!isBrowserValid) {
                showTip([[${browserInfo}]]);
            }
        [/]
    });
</script>
</html>